var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "menuId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        menu:{
            parentName:null,
            parentId:0,
            type:1,
            orderNum:0
        },
        manageMap:{}
    },
    created: function() {
        this.getManageMap();
    },
    methods: {
        getMenu: function(menuId){
            //加载菜单树
            $.get(baseURL + "sys/menu/select", function(r){
                ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
                var node = ztree.getNodeByParam("menuId", vm.menu.parentId);
                ztree.selectNode(node);

                vm.menu.parentName = node.name;
            })
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.menu = {parentName:null,parentId:0,type:1,orderNum:0};
            vm.getMenu();
        },
        update: function () {
            var menuId = getMenuId();
            if(menuId == null){
                return ;
            }

            $.get(baseURL + "sys/menu/info/"+menuId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.menu = r.menu;
                var sysManageIdStr = r.menu.sysManageId;
                $('.selectpicker').selectpicker('val',sysManageIdStr?sysManageIdStr.split(','):[]); //根据当前对象的属性，设定多选下拉框默认选择的条目
                vm.getMenu();
            });
        },
        del: function () {
            var menuId = getMenuId();
            if(menuId == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/menu/delete",
                    data: "menuId=" + menuId,
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            //取出下拉菜单选中的 sysManageId 并赋值给menu.sysManageId(menu会传给后端)
            var selectedMangeIdsStr='';
            $('.selectpicker :selected').each(function() {
                selectedMangeIdsStr+=($(this).val()+','); //字符串拼接，用英文逗号分隔
            });
            vm.menu.sysManageId=selectedMangeIdsStr.substring(0,selectedMangeIdsStr.length-1); //生成去掉末尾逗号的子字符串，并赋值给menu.sysManageId

            if(vm.validator()){
                return ;
            }

            //根据菜单类型把对应数据取出来，避免设定了某种类型独有的字段，然后切换到另一类型，导致的存储数据有误
            var menuData={};
            menuData.menuId=vm.menu.menuId;
            menuData.type=vm.menu.type;
            menuData.name=vm.menu.name;
            menuData.parentId=vm.menu.parentId;
            if(vm.menu.type===0 || vm.menu.type===1) { //类型：目录或菜单的字段
                menuData.orderNum=vm.menu.orderNum;
                menuData.icon=vm.menu.icon;
                menuData.sysManageId = vm.menu.sysManageId;
            }
            if(vm.menu.type===1 || vm.menu.type===2) { //类型：菜单或按钮的字段
                menuData.perms=vm.menu.perms;
            }
            if(vm.menu.type===1) {  //类型：菜单的字段
                menuData.url=vm.menu.url;
            }

            var url = vm.menu.menuId == null ? "sys/menu/save" : "sys/menu/update";
            $.ajax({
                type: "POST",
                url:  baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(menuData),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        menuTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.menu.parentId = node[0].menuId;
                    vm.menu.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            Menu.table.refresh();
        },
        validator: function () {
            if(isBlank(vm.menu.name)){
                alert("菜单名称不能为空");
                return true;
            }

            //菜单
            if(vm.menu.type === 1 && isBlank(vm.menu.url)){
                alert("菜单URL不能为空");
                return true;
            }
        },
        getManageMap() {
            $.get(baseURL + "sys/manage/listMap", function(r){
                vm.manageMap = r.map;
                //等到DOM更新，然后刷新selectpicker组件，解决显示不出来的问题
                vm.$nextTick(function () {
                    // DOM 现在更新了，刷新组件
                    $('.selectpicker').selectpicker('refresh');  //刷新selectpicker组件
                })
            })
        }
    }
});

var Menu = {
    id: "menuTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Menu.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '菜单ID', field: 'menuId', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '菜单名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '上级菜单', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '图标', field: 'icon', align: 'center', valign: 'middle', sortable: true, width: '80px', formatter: function(item, index){
            return item.icon == null ? '' : '<i class="'+item.icon+' fa-lg"></i>';
        }},
        {title: '类型', field: 'type', align: 'center', valign: 'middle', sortable: true, width: '100px', formatter: function(item, index){
            if(item.type === 0){
                return '<span class="label label-primary">目录</span>';
            }
            if(item.type === 1){
                return '<span class="label label-success">菜单</span>';
            }
            if(item.type === 2){
                return '<span class="label label-warning">按钮</span>';
            }
        }},
        {title: '系统管理', field: 'sysManageId', align: 'center', valign: 'middle', sortable: true, width: '100px', formatter: function(item, index){
                if(!item.sysManageId){  //item.sysManageId===0 || item.sysManageId===undefined || item.sysManageId===null
                    return '无';
                }
                var sysManageIds = item.sysManageId.split(',');
                var result = '';
                for(var index in sysManageIds) {
                    var key = sysManageIds[index];
                    if(vm.manageMap[key])
                        result+=vm.manageMap[key]+',';
                }
                result = result.substring(0, result.length-1); //去掉末尾多余逗号
                return '<span title='+result+'>'+result+'</span>';
            }},
        {title: '排序号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '菜单URL', field: 'url', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '授权标识', field: 'perms', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};


function getMenuId () {
    var selected = $('#menuTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
    var colunms = Menu.initColumn();
    var table = new TreeTable(Menu.id, baseURL + "sys/menu/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("menuId");
    table.setCodeField("menuId");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.init();
    Menu.table = table;
});
